package com.zrrd.yunchmall.user.mapper;

import com.zrrd.yunchmall.user.entity.Menu;
import com.zrrd.yunchmall.user.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 Mapper 接口
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@SuppressWarnings("all")
@Mapper
public interface RoleMapper extends BaseMapper<Role> {

    /**
     * 根据管理员ID查询角色列表
     *
     * @param AdminId 管理员ID
     * @return
     */

    @Select("SELECT * FROM ums_role WHERE id IN (SELECT role_id FROM ums_admin_role_relation WHERE admin_id=#{adminId})")
    List<Role> selectRolesByAdminId(@Param("adminId") Long AdminId);

    @Update("update ums_role_menu_relation urmr set menu_id=#{menuId} where role_id=#{id}")
    void updateMenu(String menuIds, int id);

    /**
     * 根据管理员id查询其拥有访问权限的所有菜单
     *
     * @param adminId  管理员id
     * @param isHidden 菜单的显示状态
     * @return 菜单
     */
    @Select("SELECT * FROM ums_menu " +
            "WHERE id IN " +
            "(" +
            "SELECT menu_id FROM ums_role_menu_relation WHERE role_id IN " +
            "(SELECT role_id FROM ums_admin_role_relation WHERE admin_id=#{adminId}) " +
            ") AND hidden<>#{hidden}")
    List<Menu> selectMenusByAdminId(@Param("adminId") long adminId, @Param("hidden") Integer isHidden);

    /**
     * 根据角色id查询其拥有访问权限的所有菜单
     *
     * @param roleId 角色id
     * @return 菜单
     */
    @Select("select * from ums_menu where id in (select menu_id from ums_role_menu_relation where role_id = #{roleId})")
    List<Menu> selectMenusByRoleId(@Param("roleId") long roleId);
}

